Getting Started with Amazon ECR: Creating a Repository and Pushing Docker Images

Getting Started with Amazon ECR: Creating a Repository and Pushing Docker Images

Clock Icon2024.09.12

Introduction

Hello, Hemanth from the Alliance Department here. In this blog post, I will guide you through the steps of creating an Amazon Elastic Container Registry (ECR) repository and pushing a Docker image to ECR. This process will help you securely store, manage, and deploy your Docker images using AWS’s fully managed container registry service.

AWS

Is a secure cloud service platform that offers compute power, database storage, content delivery, network, and other functionality to help businesses scale and grow. It is one of the first cloud vendors to start services in the year 2006. It offers all the 3 service models namely IAAS, PAAS, and SAAS. Some of the notable domains in AWS are Compute, Migration, Storage, Network and Content Delivery, Management Tools, Database, Messaging, Security and Identity Compliance, and many more.

Docker

Docker is a platform that allows developers to automate the deployment of applications inside lightweight, portable containers. Containers ensure consistency across multiple development and release environments, making Docker a core tool in many DevOps workflows.

ECR

Amazon ECR is a fully managed Docker container registry that helps developers store, manage, and deploy container images. It integrates seamlessly with Amazon Elastic Container Service (ECS) and Elastic Kubernetes Service (EKS), providing a streamlined workflow for containerized applications. With ECR, you can securely store Docker images and push or pull images using the AWS Command Line Interface (CLI) or other Docker-compatible tools.

Demo

Start by logging into the AWS Management Console and navigating to ECR.
Screenshot 2024-09-12 at 13.59.21
Click Create Repository.
Screenshot 2024-09-12 at 14.00.25
Give your repository a unique name and choose the necessary configurations. Click Create Repository.
Screenshot 2024-09-12 at 14.02.04
Repository is successfully created
Screenshot 2024-09-12 at 14.06.30
Configuring AWS CLI Credentials - configure your AWS CLI with credentials to interact with your AWS account.

aws configure

Provide your AWS Access Key, Secret Access Key, region, and output format as prompted.

Create a simple Dockerfile for your Docker image. This file specifies the image's base, dependencies, and commands to run.

vim dockerfile26

Press "i" to enter

FROM ubuntu:latest
RUN apt-get update && apt-get install -y \
curl \
git \
vim
WORKDIR /app
COPY . /app
CMD ["echo", "Dockerfile setup complete!"]

To ensure that unnecessary files are not included in your Docker image, create a .dockerignore

vim /Users/hemanth/.dockerignore

Click "i" to enter. I am adding all the files that are unncessary for me.

Library/
.Trash/
Desktop/
Documents/
Downloads/
Movies/
Music/
Pictures/
dockerfile/

Exclude macOS specific files
.DS_Store

Build your Docker image using the Docker CLI.

docker build -t ImageName PathToDockerFile  

Docker Image has been successfully been created
Screenshot 2024-09-12 at 14.27.41
Before pushing your image to ECR, tag it with the repository URL you obtained when you created the ECR repository.

docker tag imagename ECR-Repository-URL

Log into your ECR repository using the AWS CLI. After login it will be say successfull login.

aws ecr get-login-password --region your-region | docker login --username AWS --password-stdin ECR-Repository-URL

Push your Docker image to the ECR repository

docker push ECR-Repository-URL

After the push is complete, verify that the image is successfully listed in the ECR repository.
Screenshot 2024-09-12 at 14.47.44
Once your image is successfully pushed to ECR, clean up by removing unnecessary images and deleting the ECR repository.
List your local Docker images.

docker images  

Remove the image

docker rmi -f image-id

Go to the ECR console, delete the images inside the repository, and then delete the repository itself.
Screenshot 2024-09-12 at 15.02.05
Screenshot 2024-09-12 at 15.02.46

Conclusion

A containerized workflow that easily interfaces with other AWS services like ECS and EKS requires pushing Docker images to AWS ECR. These instructions make it simple to generate, tag, and push images to ECR, guaranteeing safe and expandable storage for your Docker images. An environment that is efficient and well-organized can be maintained by routinely clearing out obsolete pictures and repositories. You can easily and effectively manage your Docker images in the cloud with AWS ECR.

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.